var wnds = {
	
	init:function(){
		var me = this
		//初始化serverinfo读取系统信息
		this.serverInfo.load()
		//初始化files读取当前文件夹中文件
		gnp.get('php/getCurrentPath.php').then(function(r){
			me.files.load(r)
		})
		//初始化fav
		this.fav.load()
		this.pc.load()
	},
	config:{
		title:'wnds 1.0'
	},
	getFileDatas:function(list){
		var res = []
		list.forEach(function(file){
			res.push(file.data)
		})
		return res
	},
	serverInfo:{
		docRoot:'',
		httpRoot:location.origin,
		ip:"",
		load:function(){
			var that = this
			gnp.get('php/getDocRoot.php').then(function(r) {
				that.docRoot = r
			})
			gnp.get('php/getIp.php').then(function(r){
				that.ip = r
			})			
		}		
	},
	pc:{
		list:[],
		load:function(){
			var that = this
			gnp.get('php/getDisks.php').then(function(r){
				that.list = JSON.parse(r)
				that.list.shift()
				if(that.list.length>=2) {that.list.pop()}
			})
		}
	},
	files:{
		path:'',
		filter:'',
		paths:[],
		list:[],
		load:function(path){
			this.filter = ''
			var me = this			
			this.path = path
			this.paths = path.split('/')
			gnp.get('php/getFiles.php?path=' + path).then(function(r){
				var arr = JSON.parse(r)
				me.list = []
				arr.forEach(function(prop){
					prop.newname = '?'
					me.list.push({data:prop,isSelected:{value:false},el:""})					
				})				
			})
			gnp.get('php/setCurrentPath.php?path=' + path)
			wnds.selector.clear()
			sounds.play('open')
		},		
		reload:function(){
			this.load(this.path)
		},
		into:function(path){
			this.path += '/'+path
			this.load(this.path)
		},
		backto: function(path) {
			var i = this.paths.indexOf(path)
			if(i == -1 || i == this.paths.length - 1) {
				return
			}
			this.paths = this.paths.slice(0, i + 1)
			this.path = this.paths.join('/')
			this.load(this.path)			
		},
		up:function(){
			if(this.paths.length > 1){
				this.paths.pop()			 
				this.path = this.paths.join('/')
				this.load(this.path)
			}
		},		
		createfolder: function() {
			var that = this
			this.list.push({data:{				
				new: true,
				gid:getGuid(),
				img: "resource/images/file_icon/folder.png",
				basename: '新建文件夹',
				dirname: that.name,
				isSelected: false,
				filename: '新建文件夹',
				type: 'folder',
				create: function() {
					gnp.get('php/createfolder.php?name=' + this.basename).then(function(r) {
						wnds.files.reload()
					})
					this.new = false
				}
			},isSelected:{value:false},el:''})
		}
	},		
	selector:{
		isSelected:{value:true},
		list:[],
		add:function(file){
			this.clear()
			this.addin(file)			
		},
		addin:function(file){
			var ind = this.list.indexOf(file)
			if(ind > -1){
				file.isSelected = {value:false}
				this.list.splice(ind,1)
			}else{				
				this.isSelected.value=true
				this.list.push(file)
				file.isSelected = this.isSelected				
			}
		},		
		clear:function(){
			this.list = []
			this.isSelected.value = false
			this.isSelected = {value:true}
		}
		
	},
	clip:{
		method:"",
		list:[],
		copy:function(){
			this.list = wnds.selector.list
			this.method = 'copy'
		},
		cut:function(){
			this.list = wnds.selector.list
			this.method = 'cut'
		},
		paste:function(){
			if(this.list.length == 0){return}
			var files = wnds.getFileDatas(this.list)
			gnp.post('php/paste.php',{list:files,method:this.method}).then(function(r){
				wnds.files.reload()				
			})
			this.list = []
		}		
	},
	fav:{
		list:[],
		add:function(){		
			if(this.list.indexOf(wnds.files.path) > -1){return}
			this.list.push(wnds.files.path)
			gnp.post('php/setFav.php',{list:this.list})
		},
		remove:function(path){
			var ind = this.list.indexOf(path)
			if(ind > -1){
				this.list.splice(ind,1)
				gnp.post('php/setFav.php',{list:this.list})
			}
		},
		load:function(){
			var that = this
			gnp.get('php/getFav.php').then(function(r){
				that.list = JSON.parse(r)
				if(!that.list){
					that.list = []
				}
			})
		}
	},
	dialogs:{
		list: [],
		show: function(options) {
			options.kill = function() {
				var i = wnds.dialogs.list.indexOf(this)
				wnds.dialogs.list.splice(i, 1)
			}
			this.list.push(options)
	
			if(options.time) {
				setTimeout(function() {
					clearInterval(timer)
					options.kill()
				}, options.time)
			}
	
			if(options.inputs && options.inputs.length > 0) {
				options.inputs[0].autofocus = "true"
			}
			var timer = setInterval(function() {
				options.time = options.time - 100
			}, 100)
		}
	},
	defaultOptions: {
					sp:{text:''},
					glob: { 
						img: 'resource/images/menu/global.png',
						text: '用浏览器打开(enter)',
						onClicked: function() {
							var file = wnds.selector.list[0]
							var path = file.data.dirname.toLowerCase()							
							path = path.replace(wnds.serverInfo.docRoot.toLowerCase(), wnds.serverInfo.httpRoot)	
							
							if(path.indexOf('http:') > -1) {
								window.open(path + '/' + file.data.basename)
							} else {
								wnds.dialogs.show({
									content:'此目录文件不可在浏览器浏览，确定可复制到web服务器目录再浏览！',
									time:3000,
									ok:function(){
										var path = ''
										gnp.post('php/copyToTemp.php',{filename:file.data.fullname}).then(function (r) {
										    			
										})
										var wn = window.open(wnds.serverInfo.httpRoot+'/temp/' + file.data.basename,file.data.basename)
										wn.onbeforeunload = function(){
											setTimeout(function(){
												gnp.post('php/removeTempFile.php',{filename:file.data.basename}).then(function (r) {
												    l(r)												
												})	
											},1000)											
										}										
										this.kill()
									}									
								})
							}
						}
					},
					copy: { 
						img: 'resource/images/menu/copy.png',
						text: '复制(ctrl+c)',
						onClicked: function() {
							wnds.clip.copy()							
						}
					},
					cut: {
						img: 'resource/images/menu/cut.png',
						text: '剪切(ctrl+x)',
						onClicked: function() {
							wnds.clip.cut()
						}
					},
					paste: {
						img: 'resource/images/menu/paste.png',
						text: '粘贴(ctrl+v)',
						onClicked: function() {
							wnds.clip.paste()
						}
					},
					del: {
						img: 'resource/images/menu/del.png',
						text: '删除(delete)',						
						onClicked: function() {
							gnp.post('php/delete.php',{list:wnds.getFileDatas(wnds.selector.list)}).then(function(r){
								wnds.files.reload()
							})	
							sounds.play('remove')
						}
					},					
					download: {
						img: 'resource/images/menu/download.png',
						text: '下载',
						onClicked: function() {
							gnp.post('php/download.php',{list:wnds.getFileDatas(wnds.selector.list)}).then(function(r){	
								
								var ret = JSON.parse(r)
								var path = 'php/downloadfile.php?url='+ ret.url + '&path=' + ret.path 
								downloadfrom(path)
							})
						}
					},
					newfolder: {
						img: 'resource/images/menu/newfolder.png',
						text: '新建文件夹',
						onClicked: function(e) {							
							wnds.files.createfolder()
						}
					},
					rename:{
						img: 'resource/images/menu/rename.png',
						text:'重命名(F2)',
						onClicked:function(){
							if(wnds.selector.list.length != 1){return}
							var file = wnds.selector.list[0]							
							file.data.newname = file.data.basename
							file.rename = function(item){								
								if(item.newname.trim() !='' && item.newname != file.data.basename){
									
									gnp.post('php/rename.php',{file:item}).then(function(r){
										
										wnds.files.reload()
									})									
								}
								item.newname = '?'
							}
						}
					},
					zip:{
						img:'resource/images/menu/zip.png',
						text:'压缩为zip',
						onClicked:function(){
							gnp.post('php/zip.php',{list:wnds.getFileDatas(wnds.selector.list)}).then(function(r){					
								wnds.files.reload()								
							})
						}
					},
					unzip:{
						img:'resource/images/menu/unzip.png',
						text:'zip解压缩',
						onClicked:function(){
							gnp.post('php/unzip.php',{file:wnds.selector.list[0].data}).then(function(r){
								
								wnds.files.reload()
							})			
						}
					},
					qrcode:{
						img:'resource/images/menu/qrcode.png',
						text:'生成二维码',
						onClicked:function(){
							var file = wnds.selector.list[0]
							var path = file.data.dirname.toLowerCase()							
							path = path.replace(wnds.serverInfo.docRoot.toLowerCase(), wnds.serverInfo.httpRoot)	
							if(path.indexOf('localhost')){
								path = path.replace('localhost',wnds.serverInfo.ip)
							}
							if(path.indexOf('http:') > -1) {
								
								wnds.dialogs.show({
									content:path + '/' + file.data.basename,
									qrcode:path+ '/' +file.data.basename,
									ok:function(){
										this.kill()
									}
								})
								
							} else {
								wnds.dialogs.show({
									content:'此目录文件不可生成二维码，可复制到web服务器目录再生成二维码！',
									time:2000,
									ok:function(){
										this.kill()
									}									
								})
							}	
						}
					}
				}
}



wnds.init()
function downloadfrom(path){
	var elif = document.getElementById('downloader');
					
	if(!elif ){
		elif = document.createElement('iframe')
		elif.id = 'downloader'
		elif.style.display = 'none'
		document.body.appendChild(elif);
	}
	elif.src = path;  
}